/**
 * opts{
 * 		loadUrl:"",//加载内容的路径
 * 		afterFun:"",//加载完内容后执行的js
 * 		title:"",//窗口标题

 * }
 */
var DzqAsk = function (opts){
		var url = Constans.BASE_PATH+"dialogCode.jsp #xar";
		if(opts.loadUrl != null && opts.loadUrl != ""){
			url = opts.loadUrl;
		}
		var title = "分享";
		if(opts.title != null && opts.title != ""){
			title = opts.title;
		}
		new DzqWin({
			loadUrl:url,
			title:title,
			afterFun:function(){
				initAskWin(opts);
				if(typeof opts.afterFun == "function")
					opts.afterFun();
				if(typeof opts.afterFun == "string")
					eval(opts.afterFun);
			}
		});
		
		function initAskWin(){
			var $modalDialog = $("#modal-dialog");
			var $mdcontent = $("#modal-dialog-content");
			var $addQtitle = $("#addQ_title");
			var $addTags = $("#addQ_addTags");
			var $tags = $("#addQ_tags");
			var $xqq = $("#xqq");
			var $xvrw = $("#xvrw");
			var $xuq = $("#xuq");
			
			$addQtitle.val($("#searchBox").val());
			
			$("#addQ_submit").click(function(){
				$ptag=$(this).attr('tag');
		
				var contentText = $.trim(addEditor.text());
				if (contentText == "") {
					addEditor.html("");
				}
				askQuestion($ptag);
			});
		   $("#addQ_cancel").bind("click",function(){
			   $modalDialog.css("display","none");
			   $("div.modal-shadow").hide();
			   $mdcontent.find("textarea").val("");
			   $mdcontent.find("input").val("");
			   
			   $xvrw.css("display","none");
			   	$xqq.removeClass("xxzd xer xd goog-zippy-expanded").addClass("xxzd xer xd goog-zippy-collapsed");
				
			   	$xuq.css("display","none");
				$addTags.removeClass("xxzd xer xd goog-zippy-expanded").addClass("xxzd xer xd goog-zippy-collapsed");
				$("#searchList").html("");
				searchQTitleObj = null;
		   });
		   
		   $xqq.toggle(
				   function(){
					   $xvrw.css("display","block");
					    $xqq.removeClass("xxzd xer xd goog-zippy-collapsed").addClass("xxzd xer xd goog-zippy-expanded");
				   },
				   function(){
					   $xvrw.css("display","none");
					    $xqq.removeClass("xxzd xer xd goog-zippy-expanded").addClass("xxzd xer xd goog-zippy-collapsed");
				   }
				   );
		   
		   $addTags.toggle(
				   function(){
					   $xuq.css("display","block");
					   $addTags.removeClass("xxzd xer xd goog-zippy-collapsed").addClass("xxzd xer xd goog-zippy-expanded");
				  		
				  		//
				  		addQTagsListObj = $tags.wendaDropDrownList({
							"width":395,
							"topparent":"true",
							"leftparent":"true",
							//"top":$tags.parent().offset().top+$tags.parent().height(),
							//"left":$tags.parent().offset().left,
							"searchFun":"sendSearchTags",
							"enterFun":"addTagsHtml"
							});
				  	//展开时搜索
						//sendSearchTags($("#addQ_title").val());
				  	sendSearchTagsByQ($("#addQ_title").val());
				  		$tags.focus();
				   },
				   function(){
					   $xuq.css("display","none");
					    $addTags.removeClass("xxzd xer xd goog-zippy-expanded").addClass("xxzd xer xd goog-zippy-collapsed");
				   }
				 );
		   
		   /**
			 * 添加问题时的tag推荐
			 * @param keyword
			 */
			//initAddQSearchTagsBox();
			
			//分享不提示
			if(opts.type == 'ask'){
				initAddQSearchQuestionBox();
				searchQTitleObj = $addQtitle.wendaDropDrownList({
					"width":395,
					"top":$addQtitle.offset().top+$addQtitle.height(),
					"left":$addQtitle.offset().left,
					"searchFun":"sendSearchQuestion"
					//"enterFun":"enterAction"
					});
			}
			var qcontent = document.getElementById("qcontent");
			
			if(!qcontent){
				
				$("body").append('<script charset="utf-8" name="KFeditor" src="'+Constans.BASE_PATH+'kindeditor/kindeditor-min.js"><\/script>');
			}
			loadAddEditor();//加载编辑器
			
		}
		
		/**
		 * 添加问题时的问题推荐
		 * 
		 * @param keyword
		 */
		function initAddQSearchQuestionBox(){
			cacheData["listIndex"] = -1;
			$("#addQ_title").bind("click",function(){
				if($("#addQ_title").val().length > 5)
					sendSearchQuestion($("#addQ_title").val());
			});
			
			$("#addQ_title").bind("keyup",function(){
				var content = $(this).val();
				if($("#addQ_title").val().length > 4 && cacheData["qcontent"] != content){
					sendSearchQuestion(content);
					cacheData["qcontent"] = content;
					delete cacheData["redkeyword"];
				}
			});
			
		}
		
		/**
		 * 发送搜索问题请求
		 * 
		 * @param keyword
		 */
		function sendSearchQuestion(keyword){
			Wenda.post(Constans.BASE_PATH+"json/qdetailJsonAction!searchQuestions.action", {
				"keyword":keyword
			},
			function(datas){
				searchListCallBack(datas,keyword,"question");
			}, 'json');
		}
		
		/**
		 * 发送搜索tag请求
		 * 
		 * @param keyword
		 */
		function sendSearchTagsByQ(keyword){
			Wenda.post(Constans.BASE_PATH+"json/searchJsonAction!sugTagByQuestion.action", {
				"keyword":keyword
			},
			function(datas){
				searchListCallBack(datas,keyword,"tag");
			}, 'json');
		}
		
		/**
		 * 发送搜索tag请求
		 * 
		 * @param keyword
		 */
		function sendSearchTags(keyword){
			Wenda.post(Constans.BASE_PATH+"json/searchJsonAction!searchTags.action", {
				"keyword":keyword
			},
			function(datas){
				searchListCallBack(datas,keyword,"tag");
			}, 'json');
		}
		
		/**
		 * 添加问题
		 */
		function askQuestion(ptag) {
			if(DZQCheckForUser()||isSubmit)
				return;
			if(!checkAuth(DZQ_ACTIONS.ASK_QUESTION)){
				return;
			}
			
			var qtitle = $("#addQ_title").val();
			//var qcontent = $("#wtbc").val();
			var qcontent = addEditor.html();
			var domainId = 0;
			if(document.getElementById("currentdomainid")){
				domainId = document.getElementById("currentdomainid").value;
			}
			// var huati = $("#addQ_tags").val();
			if(ptag==null){
			var huati = $("#addQ_tags").val()+" ";
			var ta = $("#addQ_tagList").find("span");
			for(var i= 0;i < ta.length;i++){
				huati += $(ta[i]).text()+" ";
			}
			}else{
			var huati=ptag;
			}
			var isnm = $("#isnm").val();
			var ctype = "";
			var req_url = "json/questionJsonAction!askQuestion.action";
			if(win_url.indexOf("#share") >= 0){
				req_url = "json/questionJsonAction!addShare.action";
				ctype = 1;
			}else{
				ctype = 0;
			}
			Wenda.post(Constans.BASE_PATH+req_url, {
				"question.title" : qtitle,
				"question.content" : qcontent,
				"question.tag" : huati,
				"isAnonymous" : isnm,
				"domainId" : domainId,
				"ctype":ctype
			}, function(data) {
				if (data.isLogin == 'no') {
					window.location.href = Constans.BASE_PATH+"login.jsp";
					return;
				}
				if (data.messages != '') {
					// alert(data.messages);
					Wenda.alert(data.messages);
					return;
				}
				var questionId = data.question.id;
				if (questionId > 0) {// 问题添加成功
					// alert("问题添加成功，问题ID为" + questionId);
					if(win_url.indexOf("#share") >= 0){
						window.location.href = Constans.BASE_PATH+"question/question!fetchShareInfo.action?qid="+questionId;
					}else{
						window.location.href = Constans.BASE_PATH+"question/question!fetchQuestion.action?qid="+questionId;
					}
					
				}
			}, 'json');
		}
		
		var addEditor = null;
		function loadAddEditor(){
			var addqcontent = document.getElementById("addQcontent");
			var item = {
				resizeType : 1,
				basePath : Constans.BASE_PATH+'kindeditor/',
				themesPath : Constans.BASE_PATH+'kindeditor/themes/',
				langPath : Constans.BASE_PATH+'kindeditor/lang/',
				pluginsPath : Constans.BASE_PATH+'kindeditor/plugins/',
				cssPath : Constans.BASE_PATH+'kindeditor/plugins/code/prettify.css',
				uploadJson : Constans.BASE_PATH+'upload/uploadAction!uphoto.action?type=editor',
				allowFileManager : false,
				fieldName : 'photo',
				allowPreviewEmoticons : false,
				allowImageUpload : true,
				items : [
					'bold', 'italic', 'underline',
					'removeformat', '|', 'insertorderedlist',
					'insertunorderedlist', '|', 'emoticons',"image","insertfile","|",'link','unlink','code','fullscreen']
			};
			if(addqcontent){
				addEditor = KindEditor.create('textarea[name="addQcontent"]',item);
			}
		}
};
var searchQTitleObj = {};
var addQTagsListObj = {};